<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../plugin/layui-v2.2.5/layui/css/layui.css">
  <!-- 教师应交材料页面 -->
</head>
<body style="overflow-y: scroll;margin-top:5px">
  <div class="layui-row">
  	<div class="layui-col-md6">
	    <button id="addDepartment" data-method="addDepartment" class="layui-btn" style="float: left;margin-left: 10px">新建</button>
  	</div>
  </div>
  <div class="layui-row">
    <table class="layui-hide" id="departmentGrid" lay-filter="departmentGrid"></table>  
  </div>
  <script type="text/html" id="departmentBar">
    <a class="layui-btn layui-btn-xs" lay-event="save">保存</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </script> 
          
<script src="../plugin/layui-v2.2.5/layui/layui.js"></script>
<script src="../plugin/jquery-3.1.1.min.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>


layui.use(['table','layer','upload'], function(){
  var table = layui.table;
  
  table.render({
	    elem: '#departmentGrid'
	    ,url: '/CMA/department/getDepartmentByAcademe'
	    ,where : {
	    	"academe" : parent.getId()
	    }
	    ,method: 'post' 
	    ,done: function(res, curr, count){
	        //如果是异步请求数据方式，res即为你接口返回的信息。
	        //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
	        console.log(res);
	      }
	    ,cols: [[ //标题栏
	      {field: 'department', title: '学院名称',edit: 'text'}
	      ,{fixed: 'right', align:'center', toolbar: '#departmentBar'}
	    ]]
	    //,data: tableData
	    ,skin: 'line' //表格风格
	    ,even: true
	    ,page: true //是否显示分页
	    ,limits: [5, 7, 10]
	    ,limit: 5 //每页默认显示的数量 
	    ,id : "departmentGrid"
	 });
  
  
  //监听工具条
  table.on('tool(departmentGrid)', function(obj){
    var data = obj.data;
    console.log(data);
    //表格保存操作
    if(obj.event === 'save'){
    	$.ajax({ 
        	url: "/CMA/department/addDepartment",
        	contentType: "application/x-www-form-urlencoded",
        	type:"post",
        	dataType : "json",
        	data : data,
        	success: function(data){
        		layer.msg(data.msg, {
        		   time: 2000, //20s后自动关闭
        		});
        		//添加成功重载表格
        		table.reload('departmentGrid', {
        	  		  where: { //设定异步数据接口的额外参数，任意设
        	  			  key:null
        	  		  }
        	  		  ,page: {
        	  		    curr: 1 //重新从第 1 页开始
        	  		  }
        	  	});
        	}
          });
    } else if(obj.event === 'del'){
	      layer.confirm('真的删除行么', function(index){
	        obj.del();
	        $.ajax({ 
	        	url: "/CMA/department/delDepartment",
	        	contentType: "application/x-www-form-urlencoded",
	        	type:"post",
	        	dataType : "json",
	        	data : data,
	        	success: function(data){
	        		layer.msg(data.msg, {
	    		        time: 2000, //20s后自动关闭
	    		      });
	        	}
	          });
	        layer.close(index);
	      });
	   }
  });
  
  
  $("#addDepartment").on("click",function(){
	  table.reload('departmentGrid', {
  		  where: { //设定异步数据接口的额外参数，任意设
  		    key: "add"
  		  }
  		  ,page: {
  		    curr: 1 //重新从第 1 页开始
  		  }
  		});
  })
});


</script>

</body>
</html>